package com.common.app.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.*
import com.common.app.R
import com.common.app.adapter.CommonContactNumberAdapter
import com.common.app.adapter.CommonContactNumberAdapter.OnCommonContactNumberAdListener
import com.common.app.databinding.DialogCommonContactNumberBinding
import com.common.base.app.extras.no
import com.common.base.utils.ViewUtils


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2023/2/18
 *
 * 描述：通用电话弹框
 *
 * 修订历史：
 *
 */
class CommonContactNumberDialog(context: Context) : Dialog(context, R.style.custom_dialog_sv),
    View.OnClickListener, OnCommonContactNumberAdListener {
    private var binding: DialogCommonContactNumberBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     *
     */
    private var commonContactNumberAdapter: CommonContactNumberAdapter? = null

    /**
     * 联系电话数据
     */
    private var mList: MutableList<String> = ArrayList()

    /**
     * 点击监听
     */
    private var listener: OnCommonContactNumberListener? = null

    /**
     * 构造方法
     *
     * @param context
     */
    init {
//        super(context, R.style.VerticalDialog);
        mContext = context
    }

    //    /**
    //     * 设置监听
    //     *
    //     * @param listener
    //     */
    //    public void setOnItemClickListener(OnDialogConfirmListener listener) {
    //        this.listener = listener;
    //    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCommonContactNumberBinding.inflate(LayoutInflater.from(getContext()))
        binding?.root?.let { setContentView(it) }
        initView()
    }

    /**
     * 初始化控件
     */
    private fun initView() {
        commonContactNumberAdapter = CommonContactNumberAdapter(mContext, mList)
        binding?.let {
            it.dialogCommonContactNumberRlv.adapter = commonContactNumberAdapter
            commonContactNumberAdapter?.setOnItemClickListener(this)
            it.dialogRoot.setOnClickListener(this)
            it.dialogCommonContactNumberCloseTv.setOnClickListener(this)
        }
    }

    override fun onClick(v: View) {

        //防止多次点击
        if (ViewUtils.isFastClick()) return

        binding?.let {
            if (it.dialogRoot == v || it.dialogCommonContactNumberCloseTv == v) {
                dismiss()
            }
        }

    }

    fun setPhoneList(mList: MutableList<String>) {
        this.mList = mList
    }

    fun show(mList: MutableList<String>?) {
        super.show()
        if (mList != null) {
            this.mList = mList
        }
        commonContactNumberAdapter?.setData(this.mList)


        //获取当前Activity所在的窗体
        val dialogWindow = window

        //设置弹框的高为屏幕的一半宽是屏幕的宽
        val mDialogLp: WindowManager.LayoutParams? = dialogWindow?.attributes

        //设置宽度
        mDialogLp?.width = ViewGroup.LayoutParams.MATCH_PARENT
        mDialogLp?.height = ViewGroup.LayoutParams.MATCH_PARENT
        mDialogLp?.gravity = Gravity.CENTER //设置dialog 在布局中的位置
        dialogWindow?.attributes = mDialogLp
        dialogWindow?.setWindowAnimations(R.style.bottomShowStyle) //添加动画:
    }

    override fun dismiss() {
        super.dismiss()
    }

    fun onItemClick(view: View?, position: Int, id: Int) {
        dismiss()
    }

    /**
     * @param listener
     */
    fun setOnItemClickListener(listener: OnCommonContactNumberListener?) {
        this.listener = listener
    }

    override fun onCommonContactNumberAdClick(value: String?) {

        value.isNullOrEmpty().no {
            listener?.onCommonContactNumberClick(value)
        }
        dismiss()
    }

    /**
     *
     */
    interface OnCommonContactNumberListener {
        fun onCommonContactNumberClick(value: String?)
    }
}